package com.leyou.item.controller;


import com.leyou.common.pojo.PageResult;
import com.leyou.item.dto.SeckillGoodsDTO;
import com.leyou.item.dto.SeckillParameter;
import com.leyou.item.pojo.SeckillGoods;
import com.leyou.item.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    @PostMapping("/goods/seckill/add")
    public ResponseEntity<Void> addSeckillGoods(@RequestBody List<SeckillParameter> seckillList) {
        seckillService.addSeckillGoods(seckillList);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 分页查询秒杀商品列表
     */
    @GetMapping("/myBrand/page")
    public ResponseEntity<PageResult<SeckillGoodsDTO>> myBrandPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                   @RequestParam(value = "rows", defaultValue = "5") Integer rows,
                                                                   @RequestParam(value = "key", required = false) String key,
                                                                   @RequestParam(value = "saleable", required = false) Boolean saleable) {
        PageResult<SeckillGoodsDTO> pageResult = seckillService.myBrandPage(page, rows, key, saleable);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 秒杀商品上下架
     */
    @PutMapping("/myBrand/saleable")
    public ResponseEntity<Void> updateSaleable(@RequestParam("id") Long id,
                                               @RequestParam("saleable") Boolean saleable) {
        seckillService.updateSaleable(id, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 门户秒杀页面商品分页查询
     */
    @PostMapping("/seckill/skus/page")
    public ResponseEntity<PageResult<SeckillGoodsDTO>> findSeckillGoodsDTO(@RequestParam(value = "page", defaultValue = "1") Integer page) {
        PageResult<SeckillGoodsDTO> seckillGoodsDTOList = seckillService.findSeckillGoodsDTO(page);
        return ResponseEntity.ok(seckillGoodsDTOList);
    }

    /**
     * 秒杀商品详情页
     */
    @GetMapping("/seck/sku/{id}")
    public ResponseEntity<SeckillGoodsDTO> findSeckSkuById(@PathVariable("id") Long id) {
        SeckillGoodsDTO seckillGoodsDTO = seckillService.findSeckSkuById(id);
        return ResponseEntity.ok(seckillGoodsDTO);
    }

    /**
     * 减库存
     */
    @PutMapping("/seck/minus")
    public ResponseEntity<Void> minusSeckillStock(@RequestParam("skuId")Long skuId) {
        seckillService.minusSeckillStock(skuId);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 查询秒杀商品列表
     */
    @GetMapping("/seckill/findAllSeckill")
    public ResponseEntity<List<SeckillGoods>> findAllSeckill() {
        List<SeckillGoods> seckillGoods = seckillService.findAllSeckill();
        return ResponseEntity.ok(seckillGoods);
    }





}
